package A01Mangner;

import java.util.ArrayList;
import java.util.Scanner;

public class SMS {
    public static void Menu(){
        System.out.println("————————欢迎来到黑马学生管理系统————————");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查询学生");
        System.out.println("5.退出");
        System.out.println("请输入您的选择:");
    }
    public static Student Student_Add(){
        Scanner tempsc = new Scanner(System.in);
        Student temp = new Student();
        System.out.println("请输入学生ID");
        String ID = tempsc.next();
        temp.setId(ID);
        System.out.println("请输入学生姓名:");
        String studentName = tempsc.next();
        temp.setName(studentName);
        System.out.println("请输入学生年龄");
        int studentAge = tempsc.nextInt();
        temp.setAge(studentAge);
        System.out.println("请输入学生住址");
        String studenthome = tempsc.next();
        temp.setHome(studenthome);

        return temp;
    }
    public static void Student_Delete(String id, ArrayList<Student> students){
        if(Check(id, students)) {
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                if (student.getId().equals(id)) {
                    students.remove(student);
                }
            }
        }
        else{
            System.out.println("未查询到该用户");
        }
    }
    public static void Student_Update(String id, ArrayList<Student> students){
        Scanner tempsc = new Scanner(System.in);
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if(student.getId().equals(id)){
                System.out.println("请输入学生姓名:");
                String studentName = tempsc.next();
                student.setName(studentName);
                System.out.println("请输入学生年龄");
                int studentAge = tempsc.nextInt();
                student.setAge(studentAge);
                System.out.println("请输入学生住址");
                String studenthome = tempsc.next();
                student.setHome(studenthome);
            }
        }
    }
    public static void Student_Check(String id , ArrayList<Student> students){
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if(student.getId().equals(id)){
                System.out.println(student.getId()+student.getName()+student.getAge()+student.getHome());
            }
        }
    }
    public static boolean Check(String id , ArrayList<Student> students){
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if(student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        boolean flag = true;
        while(flag){
            Menu();
            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    Student tempstudent=Student_Add();
                    if(!Check(tempstudent.getId(),students)) {
                        students.add(tempstudent);
                    }
                    else{
                        System.out.println("该用户已存在");
                    }
                    break;
                    case 2:
                        System.out.println("请输入要删除的学生ID");
                        String deleteID = sc.next();
                        Student_Delete(deleteID,students);
                        break;
                        case 3:
                            System.out.println("请输入修改的学生ID");
                            String updateID = sc.next();
                            Student_Update(updateID,students);
                            break;
                            case 4:
                                System.out.println("请输入查询的学生ID");
                                String checkID = sc.next();
                                Student_Check(checkID,students);
                                break;
                                case 5:
                                    flag=false;
                                    break;
            }
        }
    }
}
